package com.example.a22310.myapplication.ViewModel.Post;


import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.example.a22310.myapplication.Adapter.MvvmAdapter;
import com.example.a22310.myapplication.Beans.PostDetail;
import com.example.a22310.myapplication.Listener.PostListener;
import com.example.a22310.myapplication.Model.PostModel;

import java.util.List;

public class PostDetailViewModel implements PostListener<PostDetail> {
    private ActivityPostDetailBinding binding;
    private AppCompatActivity activity;
    private int postid;


    public PostDetailViewModel(AppCompatActivity activity, ActivityPostDetailBinding binding,int postid){
        this.activity = activity;
        this.binding = binding;
        this.postid = postid;
        binding.setModel(this);
        PostModel postModel=new PostModel();
        postModel.postDetail(PostDetailViewModel.this,postid);
    }

    public void back(View view){
        activity.onBackPressed();
    }

    @Override
    public void onSuccess(PostDetail postDetail) {
        if(postDetail.getCode()!=404){
            List<PostDetail.PostlistBean> postlistBeen =  postDetail.getPostlist();
            LinearLayoutManager ms= new LinearLayoutManager(activity);

            ms.setOrientation(LinearLayoutManager.HORIZONTAL);// 设置 recyclerview 布


            binding.recyclerViewPdetail.setLayoutManager(ms);

            MvvmAdapter adapter = new MvvmAdapter(activity, R.layout.cardview_post_detail,
                    postlistBeen, BR.model);
            binding.recyclerViewPdetail.setAdapter(adapter);


        }

    }


    @Override
    public void onFailure() {
        System.out.println("进入服务器失败");
    }

}
